package com.xt.xtblesdk6;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.xt.pro.BleNFCBindInfo;
import com.xthh.XTBaseNFCActivity;
import com.xthh._enum.HH_EnumNfcErrorCode;
import com.xthh._function.HH_Logging;
import com.xthh._function.HH_SystemUtil;
import com.xthh._function._ini.HH_INI;
import com.xthh._struct.HH_T_SystemParams;

public class NFCBindActivity extends XTBaseNFCActivity implements View.OnClickListener{

    private static final String TAG = "NFCBindActivity";
    Context ctx;

    TextView textView_title;
    TextView textView_content;
    public static HandlenRecvMessage handlenRecvMessage;

    HH_T_SystemParams systemParams;
    boolean nfcBindState = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nfcbind);
        ctx = this;

        // 设置状态栏字体颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
            getWindow().setStatusBarColor(getColor(R.color.transparent));
            getWindow().setNavigationBarColor(getColor(R.color.transparent));
        }

        initView();

        handlenRecvMessage = new HandlenRecvMessage();
        handlerBaseNfc = handlenRecvMessage;


        systemParams = new HH_T_SystemParams(getApplication(), ctx, handlenRecvMessage);

        // 读NFC信息
        GetNFCCardInfo(systemParams, true);

        Thread thread = new Thread(){
            @Override
            public void run() {
                HH_SystemUtil.Sleep(500);
                Message message= handler2.obtainMessage() ;
                message.arg1 = 1;
                handler2.sendMessage(message);
            }
        };
        thread.start();
        thread = null;


        Thread thread2 = new Thread(){
            @Override
            public void run() {
                HH_SystemUtil.Sleep(10 * 1000);
                Message message= handler2.obtainMessage() ;
                message.arg1 = 2;
                handler2.sendMessage(message);
            }
        };
        thread2.start();
        thread2= null;
    }

    final Handler handler2=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            // 读NFC信息
            if (msg.arg1 == 1){
                HH_Logging.i(TAG, "GetNFCCardInfo");
                GetNFCCardInfo(systemParams, true);
            } else if (msg.arg1 == 2){
                HH_Logging.i(TAG, "连接超时");
                textView_content.setText("连接超时");
            }
        }
    };

    // 监听各种状态
    public class HandlenRecvMessage extends Handler {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case MSG_NFC_STATE:{
                    HH_EnumNfcErrorCode errorCode = HH_EnumNfcErrorCode.values()[msg.arg2];
                    switch (errorCode){
                        case BIND_INFO:     {
                            BleNFCBindInfo bindInfo = (BleNFCBindInfo)msg.obj;
                            if (bindInfo == null)
                                break;

                            if (bindInfo.deviceNumber != 113){
                                textView_content.setText("设备不匹配");
                                break;
                            }

                            String info = String.format("%d,%d,%s,%s,%s,%s,%s",
                                    bindInfo.deviceWidth, bindInfo.deviceHeight,
                                    bindInfo.deviceColor,
                                    bindInfo.mac,
                                    bindInfo.customerCode,
                                    bindInfo.customSN,
                                    bindInfo.customSRRC);
                            HH_Logging.i(TAG, info);

                            textView_content.setText(info);

                            HH_INI.putInt(systemParams, "DEVICE_NUMBER", bindInfo.deviceNumber);
                            HH_INI.putInt(systemParams, "DEVICE_W", bindInfo.deviceWidth);
                            HH_INI.putInt(systemParams, "DEVICE_H", bindInfo.deviceHeight);
                            HH_INI.putString(systemParams, "DEVICE_MAC", bindInfo.mac);
                            HH_INI.putString(systemParams, "DEVICE_CODE", bindInfo.customerCode);
                            HH_INI.putDeviceColor(systemParams, "DEVICE_COLOR", HH_SystemUtil.MadeColorType(bindInfo.deviceColor));
                            HH_INI.putString(systemParams, "DEVICE_COLOR_S", bindInfo.deviceColor);

                            HH_INI.putString(systemParams, "DEVICE_SN", bindInfo.customSN);
                            HH_INI.putString(systemParams, "DEVICE_SRRC", bindInfo.customSRRC);

                            nfcBindState = true;

                            Intent result = new Intent();
                            setResult(RESULT_OK, result);
                            finish();

                            /*deviceW = bindInfo.deviceWidth;
                            deviceH = bindInfo.deviceHeight;
                            deviceColorType = HH_SystemUtil.MadeColorType(bindInfo.deviceColor);
                            testMac = bindInfo.mac;

                            HH_INI.putInt(systemParams, "DEVICE_W", deviceW);
                            HH_INI.putInt(systemParams, "DEVICE_H", deviceH);
                            HH_INI.putString(systemParams, "DEVICE_MAC", bindInfo.mac);
                            HH_INI.putDeviceColor(systemParams, "DEVICE_COLOR", deviceColorType);

                            textView_w.setText(String.format("%d", deviceW));
                            textView_h.setText(String.format("%d", deviceH));
                            textView_mac.setText(testMac);
                            textView_color.setText(String.format("%d", deviceColorType.ordinal()));


                            if (bindInfo.deviceNumber == 113){
                                if (bindInfo.customSN != null && bindInfo.customSN.length() == 17){
                                    HH_T_SNInfo snInfo = XTCore.GetTagSNInfo(bindInfo.customSN);
                                    mAdapterCurrentTaskLogInfo.addTaskInfo(String.format("SN：项目代码=%s;手机型号=%s;款式类型=%s;年=%s;月=%s;日=%s;产品流水号=%s",
                                            snInfo.getProjectCode(),
                                            snInfo.getPhoneModel(),
                                            snInfo.getStyleType(),
                                            snInfo.getYear(),
                                            snInfo.getMonth(),
                                            snInfo.getDay(),
                                            snInfo.getSerialNumber()));
                                }
                            }
                            mAdapterCurrentTaskLogInfo.addTaskInfo(String.format("新绑定：%s,%d,%d,%d", testMac, deviceW, deviceH, deviceColorType.ordinal()));
*/
                            break;
                        }
                        case INCOMPATIBLE_TAG:{
                            //mAdapterCurrentTaskLogInfo.addTaskInfo("不兼容标签");
                            textView_content.setText("设备不匹配-不兼容标签");
                            break;
                        }
                        case ABNORMAL_TAG:{
                            //mAdapterCurrentTaskLogInfo.addTaskInfo("无效标签");
                            textView_content.setText("设备不匹配-无效标签");
                            break;
                        }
                        case DISABLE:{
                            //mAdapterCurrentTaskLogInfo.addTaskInfo("NFC 不存在或未打开");
                            textView_content.setText("设备不匹配-NFC 不存在或未打开");
                            break;
                        }
                        case ABNORMAL_DATA:{
                            //mAdapterCurrentTaskLogInfo.addTaskInfo("数据异常");
                            textView_content.setText("设备不匹配-数据异常");
                            break;
                        }
                    }

/*
                    mAdapterCurrentTaskLogInfo.notifyDataSetChanged();
                    if (mAdapterCurrentTaskLogInfo.getCount() > 0)
                        list_log_info.setSelection(mAdapterCurrentTaskLogInfo.getCount() - 1);
*/
                    break;
                }
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        finish();
        return super.onTouchEvent(event);
    }

    private void initView() {
        textView_title = findViewById(R.id.textView_title);
        textView_content = findViewById(R.id.textView_content);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        finish();
    }
}
